<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
	session_start();
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
	if($_SESSION["IsAdmin"] == true)
	{
		include_once("lib/ClassSanPham.php");
		include_once("lib/DataProvider.php");
		
		$sp = new SanPham;
		$sp->MaSanPham = SanPham::ThemVaLayMaSanPhamLonNhat();
		$sp->TenSanPham = $_POST["TenSanPham"];
		$sp->ThongTin = $_POST["ThongTin"];
		
		$root_forder = "../img/";
		//Thêm sản phẩm vào cơ sở dữ liệu	
		mkdir($root_forder.$sp->MaSanPham, 0777);
		$hinh = $root_forder.$sp->MaSanPham."/";
		
		//move_uploaded_file($_FILES['HinhAnhAvatar']["tmp_name"],$hinh.$sp->MaSanPham.".jpg");
		for($i=1;$i<=$_POST["SoLuongHinh"];$i++)
		{
			$temp = "HinhAnh".$i;
			if(isset($_FILES[$temp]["tmp_name"]))
			{
				move_uploaded_file($_FILES[$temp]["tmp_name"],$hinh.$sp->MaSanPham."_".$i.".jpg");
				echo $hinh.$sp->MaSanPham.'_'.$i.'.jpg';
				echo $temp;
				$temp_hinh = $hinh.$sp->MaSanPham.'_'.$i .'.jpg';
				$sql_img = '
					Insert into hinhanh(MaSanPham,url)
					values('.$sp->MaSanPham.',"'.$temp_hinh.'")
				';
				DataProvider::ExecuteQuery($sql_img);
			}
			else
			{
				echo "Lỗi upload hình";
				if ($_FILES[$temp]["error"] > 0)
				  {
				  echo "Error: " . $_FILES[$temp]["error"] . "<br />";
				  }
				else
				  {
					  echo "Upload: " . $_FILES[$temp]["name"] . "<br />";
					  echo "Type: " . $_FILES[$temp]["type"] . "<br />";
					  echo "Size: " . ($_FILES[$temp]["size"] / 1024) . " Kb<br />";
					  echo "Stored in: " . $_FILES[$temp]["tmp_name"];
				  }
			}
		}
		$sql = "
			Update sanpham
			Set
			TenSanPham = '".$sp->TenSanPham."',
			thongtin = '".$sp->ThongTin."'
			where MaSanPham = '".$sp->MaSanPham."'
		";
		DataProvider::ExecuteQuery($sql);
		echo $sql;
		header("Location: QuanLySanPham.php");
	}
?>
</body>
</html>